/**************************************************************
 *@ProjectName: blweibo
 *@FileName: Logger.java
 *@Date: 2011-5-30
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Date;

import android.os.Environment;

/**
 * The Class Logger.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-30
 */
public class Logger {

    /** The Constant BUFFER_SIZE. */
    private static final int BUFFER_SIZE = 102400;

    /** The bos. */
    private static BufferedOutputStream bos;

    /** The log file. */
    private static File logFile;

    static {
        File localFile = Environment.getExternalStorageDirectory();
        File path = new File(localFile.getPath() + File.separator + "zhiweibo");
        if (!path.exists()) {
            path.mkdirs();
        }
        logFile = new File(path, "logger_utf8.txt");
        if (!logFile.exists()) {
            try {
                logFile.createNewFile();
            } catch (IOException e) {
            }
        }
    }

    /**
     * D.
     * 
     * @param e
     *            the e
     */
    public static void d(Exception e) {
        StackTraceElement[] s = e.getStackTrace();
        StringBuilder sb = new StringBuilder();
        sb.append(e.getMessage()).append("\n\t");
        for (StackTraceElement st : s) {
            String strC = st.getClassName();
            if (strC.contains("com.yingnet.zhiweibo")) {
                sb.append(st.getClassName()).append("\t").append(st.getMethodName()).append("\t")
                        .append(st.getLineNumber()).append("\n\t");
            }
        }

        writingToLogFile(sb.toString());
    }

    /**
     * D.
     * 
     * @param e
     *            the e
     */
    public static void d(Throwable e) {
        StackTraceElement[] s = e.getStackTrace();
        StringBuilder sb = new StringBuilder();
        sb.append(e.getMessage()).append("\n\t");
        for (StackTraceElement st : s) {
            sb.append(st.getClassName()).append("\t").append(st.getMethodName()).append("\t")
                    .append(st.getLineNumber()).append("\n\t");
        }

        writingToLogFile(sb.toString());
    }

    /**
     * D.
     * 
     * @param paramString
     *            the param string
     */
    public static void d(String paramString) {
        writingToLogFile(paramString);
    }

    /**
     * Writing to log file.
     * 
     * @param paramString2
     *            the param string2
     */
    private static void writingToLogFile(String paramString2) {
        StringBuilder localStringBuilder1;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(logFile, true), BUFFER_SIZE);
            localStringBuilder1 = new StringBuilder();
            String str2 = MessageFormat.format("{0,date,yyyy-MM-dd HH:mm:ss.SSS}", new Date());
            localStringBuilder1.append(str2).append(": ").append(paramString2).append("\n");
            byte[] arrayOfByte = localStringBuilder1.toString().getBytes("UTF-8");
            bos.write(arrayOfByte);
            bos.flush();
            bos.close();
        } catch (FileNotFoundException localFileNotFoundException) {
        } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
        } catch (IOException localIOException) {
        }

    }
}